package com.tako;

import com.tako.utils.FileUtil;
import com.tako.utils.PDFUtil;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.concurrent.Task;

import java.io.File;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zhangyu
 * @date 2023/1/17
 */
public class MergeTask extends Task<File> {

    private File[] inputFiles;
    private File outputFile;
    private final SimpleBooleanProperty check = new SimpleBooleanProperty();
    private final SimpleStringProperty src = new SimpleStringProperty();
    private final SimpleStringProperty input = new SimpleStringProperty();
    private final SimpleIntegerProperty size = new SimpleIntegerProperty();
    private final SimpleStringProperty output = new SimpleStringProperty();
    private final SimpleStringProperty status = new SimpleStringProperty();

    public MergeTask() {
    }

    @Override
    protected File call() throws Exception {
        if (getInputFiles().length == 0) {
            setStatus("");
            return outputFile;
        }
        try {
            setStatus("转换中...");
            PDFUtil.mergeFiles(getInputFiles(), getOutputFile());
            Thread.sleep(2000);
            setStatus("转换完成");
        } catch (Exception e) {
            setStatus("转换失败," + e.getMessage());
        }
        return outputFile;
    }


    public void init(File folder) {
        if (!folder.isDirectory()) {
            throw new IllegalArgumentException();
        }
        File[] listFiles = folder.listFiles(FileUtil::support);
        this.inputFiles = listFiles == null ? new File[0] : listFiles;
        this.outputFile = Paths.get(folder.getPath() + ".pdf").toFile();
        this.setCheck(true);
        this.setSrc(folder.getName());
        String ipt = Stream.of(inputFiles)
                .map(File::getName)
                .collect(Collectors.joining(", "));
        this.setInput(ipt);
        this.setSize(inputFiles.length);
        this.setOutput(folder.getPath() + ".pdf");
        this.setStatus("等待转换");
    }

    public File[] getInputFiles() {
        return inputFiles;
    }

    public File getOutputFile() {
        return outputFile;
    }

    public boolean isCheck() {
        return check.get();
    }

    public void setCheck(boolean check) {
        this.check.set(check);
    }

    public SimpleBooleanProperty checkProperty() {
        return check;
    }

    public String getSrc() {
        return src.get();
    }

    public void setSrc(String src) {
        this.src.set(src);
    }

    public SimpleStringProperty srcProperty() {
        return src;
    }

    public String getInput() {
        return input.get();
    }

    public void setInput(String input) {
        this.input.set(input);
    }

    public SimpleStringProperty inputProperty() {
        return input;
    }

    public int getSize() {
        return size.get();
    }

    public SimpleIntegerProperty sizeProperty() {
        return size;
    }

    public void setSize(int size) {
        this.size.set(size);
    }

    public String getOutput() {
        return output.get();
    }

    public void setOutput(String output) {
        this.output.set(output);
    }

    public SimpleStringProperty outputProperty() {
        return output;
    }

    public String getStatus() {
        return status.get();
    }

    public void setStatus(String status) {
        this.status.set(status);
    }

    public SimpleStringProperty statusProperty() {
        return status;
    }
}
